package com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.composite;

import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.Quackable;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.observer.Observer;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 组合模式节点
 * @author 、
 */
public class Flock implements Quackable {

    ArrayList<Quackable> quackers = new ArrayList<>();

    public void add(Quackable quacker) {
        quackers.add(quacker);
    }

    @Override
    public void quack() {
        // 迭代器模式
        Iterator iterator = quackers.iterator();
        while (iterator.hasNext()) {
            Quackable quackable = (Quackable) iterator.next();
            quackable.quack();
        }
    }

    @Override
    public void registerObserver(Observer observer) {
        // 迭代器模式
        Iterator iterator = quackers.iterator();
        while (iterator.hasNext()) {
            Quackable quackable = (Quackable) iterator.next();
            quackable.registerObserver(observer);
        }
    }

    @Override
    public void notifyObserver() {
        // 迭代器模式
        Iterator iterator = quackers.iterator();
        while (iterator.hasNext()) {
            Quackable quackable = (Quackable) iterator.next();
            quackable.notifyObserver();
        }
    }
}
